<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2_2-属性--fillStyle</title>
</head>

<body style="display: flex; align-items: flex-start;">
    <!-- https://www.canvasapi.cn/CanvasRenderingContext2D/fillStyle -->

    <!-- 1. 色值填充 -->
    <canvas id="canvas" width="150" height="150"></canvas>
    <script>
        /** @type {HTMLInputElement} */
        var canvas = document.getElementById('canvas')
        var context = canvas.getContext('2d')
        context.fillStyle = 'red'
        context.fillRect(0, 0, 100, 100)
    </script>

    <!-- 2. 渐变填充-线性 -->
    <canvas id="canvasLinear" width="150" height="150"></canvas>
    <script>
        var canvasLinear = document.getElementById('canvasLinear')
        var contextLinear = canvasLinear.getContext('2d')
        var gradientLinear = contextLinear.createLinearGradient(0, 0, 100, 100)
        gradientLinear.addColorStop(0, 'red')
        gradientLinear.addColorStop(1, 'green')
        // 填充线性渐变
        contextLinear.fillStyle = gradientLinear
        contextLinear.fillRect(0, 0, 100, 100)
    </script>

    <canvas id="canvasRadial" width="100" height="100"></canvas>
    <script>
        var canvasRadial = document.getElementById('canvasRadial')
        var contextRadial = canvasRadial.getContext('2d')
        var gradienRadial = contextRadial.createRadialGradient(50, 50, 0, 50, 50, 50)
        gradienRadial.addColorStop(0, 'red')
        gradienRadial.addColorStop(1, 'green')
        // 填充径向渐变
        contextRadial.fillStyle = gradienRadial
        contextRadial.fillRect(0, 0, 100, 100)
    </script>
</body>

</html>